﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;

namespace DDOS.Objects
{
    public delegate int GambitCondition(GameObject self, GameObject other);

    public class Gambit
    {
        protected GambitCondition c;

        public Gambit(GambitCondition c)
        {
            this.c = c;
        }
        public virtual void Activate(GameObject self, GameObject other)
        {
            //No action taken by default
        }
        public int CheckCondition(GameObject self, GameObject other)
        {
            return c(self, other);
        }



        public static int NearestEnemy(GameObject self, GameObject other)
        {
            if (other.GetType() == typeof(Enemy0) || other.GetType() == typeof(Boss1))
            {
                if (other.active() == true)
                {
                    return (int)((float)(100 / ((Entity)self).Sight) * (self.Coordinates - other.Coordinates).Length());
                }
                else
                {
                    return 0;
                }
            }
            else
                return 0;
        }
        public static int NearestAlly(GameObject self, GameObject other)
        {
            if (other.GetType() == typeof(Minion))
            {
                return (int)((float)(100 / ((Entity)self).Sight) * (self.Coordinates - other.Coordinates).Length());
            }
            else
                return 0;
        }
        public static int TargetPlayer(GameObject self, GameObject other)
        {
            if (other.GetType() == typeof(Player))
            {
                return 100;
            }
            else
                return 0;
        }

        public static int HealthAbove75(GameObject self, GameObject other)
        {
            if (other.GetType() != typeof(Enemy0))
                return 0;
            Entity ent = ((Entity)other);
            return (int)((((100 / ent.MaxHealth) * ent.Health) - 75) * 4m);
        }
        public static int HealthAbove50(GameObject self, GameObject other)
        {
            if (other.GetType() != typeof(Enemy0))
                return 0;
            Entity ent = ((Entity)other);
            return (int)((((100 / ent.MaxHealth) * ent.Health) - 50) * 2m);
        }
        public static int HealthAbove25(GameObject self, GameObject other)
        {
            if (other.GetType() != typeof(Enemy0))
                return 0;
            Entity ent = ((Entity)other);
            return (int)((((100 / ent.MaxHealth) * ent.Health) - 25) * 1.33m);
        }

        public static int HealthBelow75(GameObject self, GameObject other)
        {
            if (other.GetType() != typeof(Enemy0))
                return 0;
            Entity ent = ((Entity)other);
            return (int)(75 - ((100 / ent.MaxHealth) * ent.Health));
        }
        public static int HealthBelow50(GameObject self, GameObject other)
        {
            if (other.GetType() != typeof(Enemy0))
                return 0;
            Entity ent = ((Entity)other);
            return (int)(50 - ((100 / ent.MaxHealth) * ent.Health));
        }
        public static int HealthBelow25(GameObject self, GameObject other)
        {
            if (other.GetType() != typeof(Enemy0))
                return 0;
            Entity ent = ((Entity)other);
            return (int)(25 - ((100 / ent.MaxHealth) * ent.Health));
        }

        public static int AllyHealthAbove75(GameObject self, GameObject other)
        {
            if (other.GetType() != typeof(Minion))
                return 0;
            Entity ent = ((Entity)other);
            return (int)((((100 / ent.MaxHealth) * ent.Health) - 75) * 4m);
        }
        public static int AllyHealthAbove50(GameObject self, GameObject other)
        {
            if (other.GetType() != typeof(Minion))
                return 0;
            Entity ent = ((Entity)other);
            return (int)((((100 / ent.MaxHealth) * ent.Health) - 50) * 2m);
        }
        public static int AllyHealthAbove25(GameObject self, GameObject other)
        {
            if (other.GetType() != typeof(Minion))
                return 0;
            Entity ent = ((Entity)other);
            return (int)((((100 / ent.MaxHealth) * ent.Health) - 25) * 1.33m);
        }

        public static int AllyHealthBelow75(GameObject self, GameObject other)
        {
            if (other.GetType() != typeof(Minion))
                return 0;
            Entity ent = ((Entity)other);
            return (int)(75 - ((100 / ent.MaxHealth) * ent.Health));
        }
        public static int AllyHealthBelow50(GameObject self, GameObject other)
        {
            if (other.GetType() != typeof(Minion))
                return 0;
            Entity ent = ((Entity)other);
            return (int)(50 - ((100 / ent.MaxHealth) * ent.Health));
        }
        public static int AllyHealthBelow25(GameObject self, GameObject other)
        {
            if (other.GetType() != typeof(Minion))
                return 0;
            Entity ent = ((Entity)other);
            return (int)(25 - ((100 / ent.MaxHealth) * ent.Health));
        }

        public override string ToString()
        {
            string retVal = "";
            if (c == NearestEnemy)
                retVal += "Nearest Enemy";
            else if (c == NearestAlly)
                retVal += "Nearest Ally";
            else if (c == TargetPlayer)
                retVal += "Target Player";
            else if (c == HealthAbove75)
                retVal += "Enemy Heath > 75%";
            else if (c == HealthAbove50)
                retVal += "Enemy Heath > 50%";
            else if (c == HealthAbove25)
                retVal += "Enemy Heath > 25%";
            else if (c == HealthBelow75)
                retVal += "Enemy Heath < 75%";
            else if (c == HealthBelow50)
                retVal += "Enemy Heath < 50%";
            else if (c == HealthBelow25)
                retVal += "Enemy Heath < 25%";
            else if (c == AllyHealthAbove75)
                retVal += "Ally Heath > 75%";
            else if (c == AllyHealthAbove50)
                retVal += "Ally Heath > 50%";
            else if (c == AllyHealthAbove25)
                retVal += "Ally Heath > 25%";
            else if (c == AllyHealthBelow75)
                retVal += "Ally Heath < 75%";
            else if (c == AllyHealthBelow50)
                retVal += "Ally Heath < 50%";
            else if (c == AllyHealthBelow25)
                retVal += "Ally Heath < 25%";
            else
                retVal += "ERR";
            return retVal + ", ";
        }

        /** BEGIN EXAMPLE USAGE
         *  public class AttackGambit : Gambit {} //with Activate override to cause self to attack other, and mirrored constructor
         *  new AttackGambit(Gambit.HealthLessThan5);
         *  END EXAMPLE USAGE **/
    }
}
